package com.vitaf.cash.app

import android.annotation.SuppressLint
import android.content.Context
import com.vitaf.cash.app.base.VitafBaseApp
import com.vitaf.cash.app.event.VitafEventPublisher
import com.vitaf.cash.app.event.room.VitafPointManager
import com.vitaf.cash.app.request.HttpHelper
import com.vitaf.cash.app.request.HttpLoader
import com.vitaf.cash.app.utils.VitafDateUtil
import com.vitaf.cash.app.utils.helper.GombeCreditAnalysisHelper
import com.vitaf.cash.app.utils.log.VitafLog
import com.vitaf.cash.app.utils.shared.VitafSharedHp
import com.vitaf.cash.app.utils.shared.VitafSharedKey

/**
 * App
 * @Author wwdeng
 * @CreateTime: 2024/11/18 13:35
 * @Annotation：
 */
class VitafCashApplication: VitafBaseApp() {
    //事件订阅
    lateinit var publisher: VitafEventPublisher

    companion object {
        @SuppressLint("StaticFieldLeak")
        lateinit var app: VitafCashApplication
    }

    override fun attachBaseContext(base: Context) {
        super.attachBaseContext(base)
        app = this
        publisher = VitafEventPublisher()
    }

    override fun onTerminate() {
        VitafPointManager.getInstance().release()
        super.onTerminate()
    }

    override fun onCreate() {
        super.onCreate()
        //保存APP启动时间
        val launchTime = VitafDateUtil.getFormatTime(System.currentTimeMillis(),"dd/MM/yyyy HH:mm:ss")
        VitafSharedHp.addGombeCreditSharedData(VitafSharedKey.GOMBECREDIT_APP_START_TIME,launchTime)

        VitafSharedHp.addGombeCreditSharedData(VitafSharedKey.GOMBECREDIT_SECRET_KEY,"Y6Er2mO2NdZe1Qm3")
        VitafSharedHp.addGombeCreditSharedData(VitafSharedKey.GOMBECREDIT_SECRET_IV,"r309qClVwLL2PDIR")
        initLibConfigs()
    }

    /**
     * 初始化Lib
     */
    private fun initLibConfigs(){
        VitafLog.debug(BuildConfig.isDebug)
//        HttpHelper.init(HttpLoader())
        HttpHelper.init(HttpLoader())
        //初始化 analysis
        GombeCreditAnalysisHelper.initAnalysisLibAtAppLaunch()
        VitafPointManager.init(this)
    }

    /**
     * 获取当前手机屏幕的宽
     */
    fun getPhoneWidth(): Int {
        // 手机屏幕的宽
        // 获取DisplayMetrics对象
        val displayMetrics = resources.displayMetrics
        return displayMetrics.widthPixels
    }

    /**
     * 获取当前手机屏幕的高
     */
    fun getPhoneHeight(): Int {
        // 手机屏幕的宽
        // 获取DisplayMetrics对象
        val displayMetrics = resources.displayMetrics
        return displayMetrics.heightPixels
    }
}